#include <stdint.h>
#include <stdio.h>
#include "lzo.h"

static void hexdump(const char *name, const uint8_t *buffer, size_t len)
{
    printf("****************%s****************\n", name);
    for (uint16_t i = 0; i < len; i++) {
        printf("%02x ", buffer[i]);
        if ((i + 1) % 16 == 0) {
            printf("\n");
        }
    }
    if (len % 16 ) {
        printf("\n");
    }
}

extern int unlzo(uint8_t *input, long in_len, uint8_t *output);

int main(int argc, char *argv[])
{
    int ret;
    const char msg[] = "hello david, hello lily, hello tom, hello lucy, hello bob";
    const char compressed_data[] = {
        0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a, 0x0a, 0x10, 0x40, 0x20, 0xa0, 0x09, 0x40, 0x03,
        0x09, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x81, 0xa4, 0x66, 0x84, 0xfc, 0x8f, 0x00, 0x00, 0x00,
        0x00, 0x04, 0x64, 0x61, 0x74, 0x61, 0x55, 0x29, 0x06, 0xa2, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00,
        0x00, 0x2a, 0x61, 0xd5, 0x14, 0x41, 0x1e, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x64, 0x61, 0x76,
        0x69, 0x64, 0x2c, 0x20, 0xb0, 0x01, 0x01, 0x6c, 0x69, 0x6c, 0x79, 0xef, 0x01, 0x74, 0x6f, 0x6d,
        0x27, 0x5a, 0x00, 0x75, 0x63, 0x27, 0x58, 0x00, 0x01, 0x62, 0x6f, 0x62, 0x0a, 0x11, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00,
    };
    // const char compressed_data[] = {
    //     0x89, 0x4c, 0x5a, 0x4f, 0x00, 0x0d, 0x0a, 0x1a,  0x0a, 0x10, 0x30, 0x20, 0x80, 0x09, 0x40, 0x03,
    //     0x09, 0x03, 0x00, 0x00, 0x01, 0x00, 0x00, 0x81,  0xa4, 0x66, 0x85, 0x34, 0x88, 0x00, 0x00, 0x00,
    //     0x00, 0x04, 0x64, 0x61, 0x74, 0x61, 0x47, 0x57,  0x05, 0xa4, 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00,
    //     0x00, 0x2a, 0x61, 0xd5, 0x14, 0x41, 0x1e, 0x68,  0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x64, 0x61, 0x76,
    //     0x69, 0x64, 0x2c, 0x20, 0xb0, 0x01, 0x01, 0x6c,  0x69, 0x6c, 0x79, 0xef, 0x01, 0x74, 0x6f, 0x6d,
    //     0x27, 0x5a, 0x00, 0x75, 0x63, 0x27, 0x58, 0x00,  0x01, 0x62, 0x6f, 0x62, 0x0a, 0x11, 0x00, 0x00,
    //     0x00, 0x00, 0x00, 0x00,
    // };
    char buf[256] = {0};

    ret = unlzo((uint8_t*)compressed_data, sizeof(compressed_data), buf);
    if (ret != 0) {
        printf("internal error - decompression failed: %d\n", ret);
    }

    printf("%s\n", buf);

    return 0;
}

// lzop -9 -f -v data -o data.lzo